--
-- Author: yawei.zhu
-- Date: 2018-09-05 16:58:51
--
-- PrestigeShopController
--

local PrestigeShopView = import("..views.PrestigeShopView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("PrestigeShopModel")
    self._walletHandler = UD:onChanged("wallet", handler(self, self.updatePlayer))
    self._itemChangeHandler = UD:onChanged("bag", handler(self, self.updateItem))
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
    UD:unChanged("wallet", self._walletHandler)
    UD:unChanged("bag", self._itemChangeHandler)
end

function ClassRef:onEvent(eventName, data)
end

function ClassRef:listEvent()
    return {}
end

function ClassRef:openView(data)
    self._returnCallBack = data.returnCallBack
    
    if cc.isDead(self._view) then
        self._view = PrestigeShopView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        self:addChild(self._view, data)
    end

    self._view:show()
    if data then
        self._view:updatePanelInfo(data)
    end
end

function ClassRef:closeView(data)
    if self._view then
        self:removeChild(self._view, data)
        self:clearView()
    end
end

function ClassRef:clearView()
    if self._view then
        self._view = nil
    end
end

-------------------------------------------------------


-------------------------------------------------------
function ClassRef:onOperate(op, data)
    print("PrestigeShopController:onOperate op = " .. op)
    if op == "closeUI" then
        self:closeView()
        if self._returnCallBack then
            executeFunc(self._returnCallBack)
        end
    end
end

function ClassRef:updatePlayer(name, old, new)
    if not self:isViewAlive() then return end
    if name == "magicDust" or name == "gold" then
        self._view:updateGoldInfo()
    end
end

function ClassRef:updateItem()
    if not self:isViewAlive() then return end
    self._view:updateGoldInfo()
end

return ClassRef
